package com.ptit.giaothong.help;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;

import com.ptit.giaothong.DelayActivity;
import com.ptit.giaothong.GlobalData;
import com.ptit.giaothong.MainActivity;
import com.ptit.giaothong.R;
import com.ptit.giaothong.database.DatabaseHelper;
import com.ptit.giaothong.taxi.itemtaxi;

public class HelpActivity extends Activity {
	private ListView listview;
	private List<itemtaxi> listBig;
	private List<itemtaxi> listtmp;
	private Activity activity;
	public int pagageIndex = 0;
	private ItemAdapter itemAdapter;

	/** Called when the activity is first created. */
	public HelpActivity(Activity activity) {
		this.activity = activity;
		init();

	}

	public void init() {
		MainActivity.keypage = 1;
		activity.setContentView(R.layout.cuuho);
		TextView title = (TextView) activity.findViewById(R.id.textView1);
		title.setText("Trung tâm cứu hộ giao thông");

		Cursor c = DelayActivity.myDbHelper.query(DatabaseHelper.TB_QUESTION,
				null, null, null, null, null, null);

		String sql = "select * from " + DatabaseHelper.TB_CUUHO;
		c = DelayActivity.myDbHelper.rawQuery(sql);

		listBig = new ArrayList<itemtaxi>();

		if (c.moveToFirst()) {
			do {
				itemtaxi item = new itemtaxi();
				item.setName(c.getString(1));
				item.setNumphone(c.getString(2));
				item.setMavung(c.getString(3));
				item.setTentinh(c.getString(4));
				listBig.add(item);

			} while (c.moveToNext());
		}

		listview = (ListView) activity.findViewById(R.id.listViewbb);

		itemAdapter = new ItemAdapter(activity, R.layout.itemcuuho, listBig);
		listview.setAdapter(itemAdapter);
		listview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1,
					int postion, long arg3) {
				call(listBig.get(postion).getNumphone());
			}
		});

		final String[] tinh = activity.getResources().getStringArray(
				R.array.tinh);
		Spinner tentinh = (Spinner) activity.findViewById(R.id.Spinner02);
		GlobalData.setAdapterSpinner(activity, tentinh, tinh);
		tentinh.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int position, long arg3) {
				listtmp = getListData(1, tinh[position]);

				itemAdapter = new ItemAdapter(activity, R.layout.itemcuuho,
						listtmp);
				listview.setAdapter(itemAdapter);
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub

			}
		});

		String[] listLoaibien = activity.getResources().getStringArray(
				R.array.tenmien);
		Spinner spinner = (Spinner) activity.findViewById(R.id.spinner1);
		GlobalData.setAdapterSpinner(activity, spinner, listLoaibien);

		spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int position, long arg3) {
				listtmp = getListData(2, String.valueOf(position + 1));
				itemAdapter = new ItemAdapter(activity, R.layout.itemcuuho,
						listtmp);
				listview.setAdapter(itemAdapter);
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub

			}
		});

	}

	public List<itemtaxi> getListData(int option, String key) {
		List<itemtaxi> listsearch = new ArrayList<itemtaxi>();

		for (int i = 0; i < listBig.size(); i++) {
			if (option == 1) {
				if (listBig.get(i).getTentinh().equalsIgnoreCase(key)) {
					listsearch.add(listBig.get(i));
				}
			} else {
				if (listBig.get(i).getMavung().equalsIgnoreCase(key)) {
					listsearch.add(listBig.get(i));
				}
			}
		}
		return listsearch;
	}

	public void call(String numphone) {
		try {
			Intent callIntent = new Intent(Intent.ACTION_CALL);
			callIntent.setData(Uri.parse("tel:" + numphone));
			activity.startActivity(callIntent);
		} catch (ActivityNotFoundException activityException) {

		}
	}

	static class ItemAdapter extends ArrayAdapter<itemtaxi> {
		private int resource;
		private LayoutInflater layoutInflater;

		public ItemAdapter(Context mContext, int resource, List<itemtaxi> models) {
			super(mContext, resource, models);
			this.resource = resource;
			layoutInflater = LayoutInflater.from(mContext);
		}

		public class ViewHolder {
			public TextView numphone;
			public TextView name;

		}

		@Override
		public View getView(final int position, View convertView,
				ViewGroup parent) {
			ViewHolder viewHolder;
			if (convertView == null) {
				viewHolder = new ViewHolder();
				convertView = layoutInflater.inflate(resource, null, true);
				viewHolder.name = (TextView) convertView
						.findViewById(R.id.nametxt);
				viewHolder.numphone = (TextView) convertView
						.findViewById(R.id.txtnumphone);
				convertView.setTag(viewHolder);
			} else {
				viewHolder = (ViewHolder) convertView.getTag();
			}
			itemtaxi item = getItem(position);
			viewHolder.name.setText(item.getName());
			viewHolder.numphone.setText("Điện thoại:" + item.getNumphone());
			return convertView;
		}
	}

	@Override
	public void onBackPressed() {
		switch (pagageIndex) {
		case 0:
			MainActivity.getdialogMesg(activity, "Bạn có thực sự muốn thoát");
			break;
		case 1:
			init();
			break;

		}
	}
}
